<?php

class SitesKeyword extends Base_Record
{ 
    static protected $validate = array(
      'keyword' => array('notEmpty','plainText'),
      );
    static protected $names = array(
      'keyword' => 'Keyword',
      );
    static protected $descriptions = array(
      'keyword' => 'Keyword tracked for site',
      );
          
    static public function getVarName( $key )
    {
      return isset(self::$names[$key]) ? self::$names[$key] : null;
    }
    
    static public function getVarDescription( $key )
    {
      return isset(self::$descriptions[$key]) ? self::$descriptions[$key] : null;
    }    
    
    protected function &getValidationScheme()
    {
      return self::$validate;
    }
    
    public function load($id)
    {
        $this->_import('sites_keywords',array('idKeyword'=>$id));
        return $this;
    }
    
    public function getID()
    {
      return $this->idKeyword;
    }

    public function delete()
    {
        $query = $this->database->prepare('DELETE FROM `sites_keywords` WHERE `idKeyword` = :id');
        $query->bindParam(':id', $this->getID(), PDO::PARAM_INT);
        $query->execute();
        
        $query = $this->database->prepare('DELETE FROM `sites_data_keywords` WHERE `idKeyword` = :id');
        $query->bindParam(':id', $this->getID(), PDO::PARAM_INT);
        $query->execute();        
        
        $this->idKeyword = null;

        return $this;
    }
    
    public function setSite( $id )
    {
      if( $id instanceof Site ) {
        $this->idSite = $id->getID();
      } else {
        $this->idSite = (int)$id;
      }
    }
    
    /**
     * @return Account
     * @version 0.0.1
     * @since 0.0.1
     */
    public function save()
    {
      if( $this->validate() === false ) {
        throw new Base_RecordException('Cannot save invalid data');
      }
      $vars = $this->getModelVars();
      $id = isset($vars['idSitesKeyword']) ? $vars['idSitesKeyword'] : null;
      unset($vars['idSitesKeyword']);
      if( $id ) {
        $this->_update('sites_keywords',$vars,array('idSitesKeyword'=>$id));
      } else {
        $this->_insert('sites_keywords',$vars);
        $this->id = $this->database->lastInsertId();
      }
      return $this;
    }
      
}